home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / PhaseShiftX / Source / Shared / DebugUtils.cp next >
Encoding:
Text File  |  2001-06-23  |  1.8 KB  |  84 lines

  1. #include <ctype.h>
  2. #include "DebugUtils.h"
  3.  
  4.  
  5.  
  6. void printmem(const void *data,size_t len)
  7. {
  8.     fprintmem(stdout,data,len);
  9. }
  10.  
  11.  
  12.  
  13. void fprintmem(FILE *stream,const void *data,size_t len)
  14. {
  15.     static char        sBin2Hex[] = "0123456789ABCDEF";
  16.     static char        sPrintTable[256];
  17.     static Boolean    sNeedInit = true;
  18.     char            ascii[17],hex[41],*hstr,*astr;
  19.     Boolean            partial = (len < 16);
  20.     UInt32            spaces,index;
  21.     UInt8            byte;
  22.     
  23.     if (sNeedInit)
  24.     {
  25.         for (index = 0;index < 256;index++)
  26.             sPrintTable[index] = (index & 0x80) ? '.' : isprint(index) ? index : '.';
  27.         
  28.         sNeedInit = false;
  29.     }
  30.     
  31.     // Print header line.
  32.     fprintf(stream,"Displaying %lu bytes from %08lX\n",len,(UInt32)data);
  33.     
  34.     // Print full lines.
  35.     for (ascii[16] = '\0';len >= 16;len -= 16,(UInt8*)data += 16)
  36.     {
  37.         for (index = 0;index < 16;index++)
  38.             ascii[index] = sPrintTable[*((UInt8*)data + index)];
  39.         
  40.         fprintf(stream,"%08lX  %04X %04X %04X %04X  %04X %04X %04X %04X  '%s'\n",(UInt32)data,*((UInt16*)data + 0),
  41.                 *((UInt16*)data + 1),*((UInt16*)data + 2),*((UInt16*)data + 3),*((UInt16*)data + 4),
  42.                 *((UInt16*)data + 5),*((UInt16*)data + 6),*((UInt16*)data + 7),ascii);
  43.     }
  44.     
  45.     // Print remainder/partial line.
  46.     if (len > 0)
  47.     {
  48.         spaces = 0;
  49.         hstr = &hex[0];
  50.         astr = &ascii[0];
  51.         for (index = 0;index < len;index++)
  52.         {
  53.             spaces = 0;
  54.             byte = *((UInt8*)data + index);
  55.             *astr++ = sPrintTable[byte];
  56.             *hstr++ = sBin2Hex[byte >> 4];
  57.             *hstr++ = sBin2Hex[byte & 15];
  58.             
  59.             if ((index & 7) == 7)
  60.             {
  61.                 *hstr++ = ' ';
  62.                 spaces += 1;
  63.             }
  64.             
  65.             if ((index & 1) == 1)
  66.             {
  67.                 *hstr++ = ' ';
  68.                 spaces += 1;
  69.             }
  70.         }
  71.         
  72.         while(spaces < 2)
  73.         {
  74.             *hstr++ = ' ';
  75.             spaces += 1;
  76.         }
  77.         
  78.         *hstr = '\0';
  79.         *astr = '\0';
  80.         
  81.         fprintf(stream,"%08lX  %s%*s'%s'\n",(UInt32)data,hex,(int)(partial ? 0 : (42 - strlen(hex))),"",ascii);
  82.     }
  83. }
  84.